//
//  GLFormOptionSelectCell.h
//  KTools
//
//  Created by zhiyong.kuang on 2017/9/22.
//  Copyright © 2017年 zhiyong.kuang. All rights reserved.
//
//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                            O\ = /O
//                        ____/`---'\____
//                      .   ' \\| |// `.
//                       / \\||| : |||// \
//                     / _||||| -:- |||||- \
//                       | | \\\ - /// | |
//                     | \_| ''\---/'' | |
//                      \ .-\__ `-` ___/-. /
//                   ___`. .' /--.--\ `. . __
//                ."" '< `.___\_<|>_/___.' >'"".
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |
//                 \ \ `-. \_ __\ /__ _/ .-` / /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//
//         .............................................
//                  佛祖镇楼                  BUG辟易

#import "GLFormOptionSelectCell.h"
#import <Masonry/Masonry.h>

#define magin 10
#define attentionBtnH 19
#define attentionBtnMagin 12
#define containerViewW ([UIScreen mainScreen].bounds.size.width-100)

@interface GLFormOptionSelectCell()
/** 内容 */
@property (nonatomic, weak) UIView *containerView;
/** 选中的购车关注数组 */
@property (nonatomic, strong) NSMutableArray *selectedArray;
@end

@implementation GLFormOptionSelectCell
- (NSMutableArray *)selectedArray
{
    if (!_selectedArray) {
        _selectedArray = [[NSMutableArray alloc] init];
    }
    return _selectedArray;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.textColor = [UIColor grayColor];
        _titleLab.font = [UIFont systemFontOfSize:16];
        [self.contentView addSubview:_titleLab];
        [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(70);
            make.top.equalTo(self).offset(15);
            make.left.equalTo(self).offset(15);
        }];
        
        UIView *containerView = [[UIView alloc] init];
        self.containerView = containerView;
        [self.contentView addSubview:containerView];
        [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_titleLab);
            make.left.equalTo(_titleLab.mas_right).offset(magin);
            make.width.mas_equalTo(containerViewW);
            make.bottom.equalTo(self).offset(-magin);
        }];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setDatas:(NSArray *)array
{
    _datas = [array copy];
    for (UIView*v in self.containerView.subviews) {
        [v removeFromSuperview];
    }
    
    CGFloat attentionBtnW = containerViewW/3.0f;
    CGRect newFrame = self.containerView.frame;
    newFrame.size.height = ceilf(_datas.count/3.0f) * (19+12);
    self.containerView.frame = newFrame;
    for (int i = 0 ; i < _datas.count ; i++) {
        int row = i/3;//行号
        int col = i%3;//列号
        CGFloat btnX = attentionBtnW *col;
        CGFloat btnY = (attentionBtnH + magin) *row;
        NSString* title = _datas[i];
        UIButton *attentionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        attentionBtn.tag = i;
        [attentionBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        attentionBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        attentionBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
        attentionBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
        attentionBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        attentionBtn.titleLabel.adjustsFontSizeToFitWidth = true;
        [attentionBtn setImage:[UIImage imageNamed:@"icon-checkbox-selected"] forState:UIControlStateSelected];
        [attentionBtn setImage:[UIImage imageNamed:@"icon-checkbox"] forState:UIControlStateNormal];
        [attentionBtn setTitle:title forState:UIControlStateNormal];
        [attentionBtn setTitle:title forState:UIControlStateSelected];
        [attentionBtn addTarget:self action:@selector(attentionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.containerView addSubview:attentionBtn];
        attentionBtn.frame = CGRectMake(btnX, btnY, attentionBtnW, attentionBtnH);
        attentionBtn.selected = [self valueIsSelected:title];
    }
}

- (BOOL)valueIsSelected:(NSString*)value{
    if (!value) {
        return nil;
    }
    for(NSString *str in self.selectedArray) {
        if ([value isEqualToString:str]) {
            return YES;
        }
    }
    return NO;
}

- (void)attentionBtnClick:(UIButton *)btn{
    btn.selected = !btn.selected;
    
    NSString* title = self.datas[btn.tag];
    if (btn.selected) {//添加到数组中
        [self.selectedArray addObject:title];
    }else{//删除
        for (int i = 0 ; i<self.selectedArray.count;i++) {
            NSString *key = self.selectedArray[i];
            if ([key isEqualToString:title]) {
                [self.selectedArray removeObjectAtIndex:i];
            }
        }
    }
    [self valueDidChanged:self.selectedArray];
}

/*
 * need override
 */

-(void)setRowValue:(id)value{
}
/*
 *need call
 */
//-(void)valueDidChanged:(id)newValue;
//-(void)clicked:(id)sender subTag:(NSString*)tag;

@end
